package com.automobile.dao;

import com.automobile.domain.Announce;
import com.automobile.domain.Smart;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 类的作用
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-03-24 15:29
 */
public interface CollectDao {

    @Select("select id,title,colour,money,carBrandId,userId from announce where id in (select announceId from collect where userId = #{userId} and smartId is null)")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "title",column = "title"),
            @Result(property = "colour",column = "colour"),
            @Result(property = "money",column = "money"),
            @Result(property = "carBrand",column = "carBrandId",one = @One(select = "com.automobile.dao.CarBrandDao.findBrandNameById",fetchType = FetchType.LAZY)),
            @Result(property = "userName",column = "userId",one = @One(select = "com.automobile.dao.UserDao.findUserNameById",fetchType = FetchType.LAZY)),
            @Result(property = "imgs",column = "id",one = @One(select = "com.automobile.dao.ImgDao.findImgs",fetchType = FetchType.LAZY)),
    })
    List<Announce> findCollectByUserId(int userId);

    @Select("select id,title,num,money,userId from smart where id in (select smartId from collect where userId = #{userId} and announceId is null)")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "title",column = "title"),
            @Result(property = "num",column = "num"),
            @Result(property = "money",column = "money"),
            @Result(property = "userName",column = "userId",one = @One(select = "com.automobile.dao.UserDao.findUserNameById",fetchType = FetchType.LAZY)),
            @Result(property = "imgs",column = "id",one = @One(select = "com.automobile.dao.ImgDao.findImgs2",fetchType = FetchType.LAZY)),
    })
    List<Smart> findCollectByUserId2(int userId);


    @Select("select count(*) from collect where userId = #{userId} and announceid = #{announceId}")
    int findCollect(@Param("userId") int userId, @Param("announceId") int announceId);

    @Delete("delete from collect where userId = #{userId} and announceId = #{announceId}")
    void deleteCollect(@Param("userId") int userId, @Param("announceId") int announceId);

    @Insert("insert into collect(userId,announceId) values(#{userId},#{announceId})")
    void save(@Param("userId") int userId, @Param("announceId") int announceId);

    @Select("select count(*) from collect where userId = #{userId} and smartId = #{smartId}")
    int findCollect2(@Param("userId") int userId, @Param("smartId") int smartId);

    @Delete("delete from collect where userId = #{userId} and smartId = #{smartId}")
    void deleteCollect2(@Param("userId") int userId, @Param("smartId") int smartId);

    @Insert("insert into collect(userId,smartId) values(#{userId},#{smartId})")
    void save2(@Param("userId") int userId, @Param("smartId") int smartId);

}
